package com.cloud.admin.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.admin.beans.dto.UserDTO;
import com.cloud.admin.beans.po.User;
import com.cloud.admin.beans.qo.UserQO;
import com.cloud.admin.feign.IUserService;
import com.cloud.admin.service.UserService;
import com.cloud.common.base.Result;
import com.cloud.common.exception.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import com.cloud.common.base.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Aijm
 * @since 2019-05-04
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
@Api(value = "用户管理模块")
@Log
public class UserController extends BaseController implements IUserService {

    private UserService userService;

    /**
     * 获取当前用户详细信息
     * @return
     */
    @GetMapping(value = {"/getUser"})
    public Result<User> getUser(HttpServletRequest request) {

        SecurityContext context = SecurityContextHolder.getContext();
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String loginName = "";
        User sysUser = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getLoginName, loginName));
        return Result.success(sysUser);

    }


    /**
     * 获取指定用户全部信息
     * @param loginName
     * @return  用户详细信息
     */
    @Override
    @RequestMapping(value="/info/{loginName}", method= RequestMethod.GET)
    public Result<UserDTO> info(@PathVariable("loginName") String loginName) {
        User user = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getLoginName, loginName));
        if (user == null) {
            return Result.error("用根据用户名查询不到用户");
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        log.info(userDTO.getName());
        return Result.success(userDTO);
    }


    @Override
    @RequestMapping(value="/info/phone/{mobile}", method= RequestMethod.GET)
    public Result<UserDTO> infoByMobile(@PathVariable("mobile") String mobile) {
        User user = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getPhone, mobile));
        if (user == null) {
            return Result.error("根据手机号查不到用户");
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return Result.success(userDTO);
    }

    /**
     * 根据用户查询条件查询
     * @param page 页数
     * @param userQO 查询条件
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "列表", notes = "传入role", position = 2)
    public Result getPage(Page page, UserQO userQO) {
        return Result.success(userService.page(page, Wrappers.query(userQO)));
    }

    /**
     * 新增
     * @param userDTO 用户的详细信息
     * @return 新增后的用户详细信息
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "传入User", position = 3)
    public Result<UserDTO> add(@Valid @RequestBody UserDTO userDTO){
        return null;
    }


    /**
     * 修改
     * @param userDTO 修改的用户信息
     * @return 修改后用户详细信息
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "传入User", position = 3)
    public Result<UserDTO> update(@Valid @RequestBody UserDTO userDTO){
        return null;
    }


    /**
     *  删除
     * @param ids  id的集合 ',' 分隔开
     * @return 是否成功
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除", notes = "传入ids", position = 4)
    public Result delete(@RequestParam String ids){
        return null;
    }




}


